package com.woniuxy.project.campus.technical.config;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;

public class SMSUtil {
    @Bean
    public String sendCode(String mobile)throws Exception{
         String api ="https://106.ihuyi.com/webservice/sms.php?method=Submit";
         String account="C09844997";
         String password="7e71a60be5b693f8258d7e987316f124";
        System.out.println("手机号"+mobile);
        System.out.println("api"+api);
        System.out.println("account"+account);
        System.out.println("password"+password);

        //使用hutool提供的Http工具发送请求
        HashMap<String, Object> params = new HashMap<>();
        params.put("account",account);
        params.put("password",password);
        params.put("mobile",mobile);
        params.put("format","json");
        String code=String.valueOf((int)(Math.random()*9000)+1000);
        System.out.println(code);
        String content="您的验证码是：" + code + "。请不要把验证码泄露给其他人。";
        params.put("content",content);

        //向第三方的接口发送请求
        HttpResponse response = HttpRequest.post(api).form(params).execute();
        String json = response.body();
        String resultCode = JSONUtil.parseObj(json).get("code").toString();
        System.out.println(resultCode);
        if("2".equals(resultCode)){
            return code;
        }else{
            throw new Exception("短信发送失败");
        }
    }
}
